import { defineStore } from "pinia";
const mockFetchProducts = () => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve([
        {
          id: 1,
          name: "粉蒸肉",
          price: 32,
          quantity: 0,
          categoryId: 1,
          score: 3,
          image: "../../static/粉蒸排骨.jpg",
        },
        {
          id: 2,
          name: "红三剁",
          price: 35,
          quantity: 3,
          categoryId: 1,
          score: 3,
          image: "../../static/10.jpg",
          desc: "红三剁是一道云南特色家常菜，以其酸香开胃，颜色亮丽而闻名，主要食材包括西红柿，青椒和肉沫",
        },
        {
          id: 3,
          name: "拿铁咖啡",
          price: 32,
          quantity: 0,
          categoryId: 2,
          score: 4,
          image: "../../static/粉蒸排骨.jpg",
        },
        {
          id: 4,
          name: "番茄牛腩",
          price: 32,
          quantity: 0,
          categoryId: 1,
          score: 5,
          image: "../../static/2.jpg",
        },
        {
          id: 5,
          name: "芦笋虾仁",
          price: 35,
          quantity: 3,
          categoryId: 1,
          score: 3,
          image: "../../static/3.jpg",
        },
        {
          id: 6,
          name: "拿铁咖啡",
          price: 32,
          quantity: 0,
          categoryId: 3,
          score: 2,
          image: "../../static/5.jpg",
        },
        {
          id: 7,
          name: "摩卡咖啡",
          price: 35,
          quantity: 0,
          categoryId: 3,
          score: 3,
          image: "../../static/4.jpg",
        },
        {
          id: 8,
          name: "玉米浓汤",
          price: 35,
          quantity: 3,
          categoryId: 4,
          score: 4,
          image: "../../static/1.jpg",
        },
        {
          id: 9,
          name: "拿铁咖啡",
          price: 32,
          quantity: 0,
          categoryId: 4,
          score: 2,
          image: "../../static/粉蒸排骨.jpg",
        },
        {
          id: 10,
          name: "摩卡咖啡",
          price: 35,
          quantity: 0,
          categoryId: 5,
          score: 3,
          image: "../../static/7.jpg",
        },
        {
          id: 10,
          name: "摩卡咖啡",
          price: 35,
          quantity: 0,
          categoryId: 5,
          score: 3,
          image: "../../static/6.jpg",
        },
        {
          id: 10,
          name: "摩卡咖啡",
          price: 35,
          quantity: 0,
          categoryId: 6,
          score: 4,
          image: "../../static/9.jpg",
        },
        {
          id: 10,
          name: "摩卡咖啡",
          price: 35,
          quantity: 0,
          categoryId: 6,
          score: 5,
          image: "../../static/8.jpg",
        },
      ]);
    }, 1000);
  });
};

export const useProductStore = defineStore("product", {
  state: () => ({
    products: [],
    loading: false,
    error: null,
  }),

  actions: {
    async fetchProducts() {
      this.loading = true;
      try {
        const data = await mockFetchProducts();
        this.products = data;
      } catch (error) {
        this.error = error;
      } finally {
        this.loading = false;
      }
    },

    //TODO：创建商品
    // async createProduct(product) {

    // },

    getProductById(id) {
      return this.products.find((product) => product.id === id);
    },
    deleteProduct(id) {
      const index = this.products.findIndex((p) => p.id === id);
      if (index !== -1) {
        this.products.splice(index, 1);
      }
    },
  },

  getters: {
    totalProducts: (state) => state.products.length,
  },
  persist: {
    enabled: true,
  },
});
